﻿DataList = function ()
{
  this.data = new Array();

  this.onAdded = function ()
  {

  }

  this.onDeleted = function ()
  {
  }

  this.onModified = function ()
  {

  }

  this.SetData = function (newData)
  {
    this.data = newData;
    this.onModified(newData);
  }

  this.Count = function ()
  {
    return this.data.length;
  }

  this.Add = function (model)
  {
    var x = this.data.length;
    this.data[this.data.length] = model;
    this.onAdded(this.data[x]);
    this.onModified(this.data[x]);
  }

  this.Delete = function (iid, item)
  {
    if (iid != null)
    {
      for (var i = 0; i < this.data.length; i++)
      {
        if (this.data[i].id == iid)
        {
          var t = this.data[i];
          var deletedItem = this.data.splice(i, 1);
          try
          {
            delete deletedItem;
          } catch (e) { } //ie8 throws an error here
          this.onDeleted(t, item);
          this.onModified(t);
          break;
        }
      }
    }
  }
}
